var message_num  = 0;

//展示消息框
function showMessage(){
    layui.use('layer',function () {
        layer.tab({
            skin: 'layui-layer-moon', //样式类名
            area: ['600px', '350px'],
            tab: [{
                title: '消息通知',
                content: '<div id="message"><div id="likes"></div><div id="collection"></div><div id="comment"></div></div>'
            }]
        });
    });
    getLikesMessage();getCollectionMessage();getCommentMessage();
    getMessageNum();
    if(message_num === 0){
        $('#message').html("<fieldset style='margin-top: 5%' class=\"layui-elem-field\">\n" +
            "  <legend >暂无消息通知</legend>\n" +
            "  <div class=\"layui-field-box\" style='text-align: right'>\n" +
            "    快去<a onclick='skip_to(PUBLISH_INDEX,\"\")' style='text-decoration: none;color: #00b4ef' href=\"javascript:void(0)\">发帖</a>赚取更多关注与点赞吧\n" +
            "  </div>\n" +
            "</fieldset>");
    }else {
        getLikesMessage();
    }
}

//获取点赞消息
function getLikesMessage() {
    let obj = get_message_likes(UID);
    var str =""
    $.each(obj.data,function (index,item) {
        str += ' <fieldset class="layui-elem-field layui-field-title">\n' +
            '        <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px" href="/home/visitor/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;在&nbsp;</a><span style="font-size: 8px"><a href="/home/post/'+item.link+'">'+item.content+'</a>&nbsp;中赞了你</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageLike(this.id)">知道啦</button></span></legend>\n' +
            '    </fieldset>';
    });
    $("#likes").html(str);
}
//获取关注消息
function getCollectionMessage() {
    let obj = get_message_collection(UID);
    var str ="";
    $.each(obj.data,function (index,item) {
        str +='<fieldset class="layui-elem-field layui-field-title">\n' +
            '            <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px"  href="/home/visitor/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;&nbsp;</a><span style="font-size: 8px">&nbsp;关注了你</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageCollection(this.id)">知道啦</button></span></legend>\n' +
            '        </fieldset>';
    });
    $("#collection").html(str);

}
//获取评论与回复消息
function getCommentMessage() {
    let obj = get_message_comment(UID);
    var str ="";
    $.each(obj.data,function (index,item) {
        str +='  <fieldset class="layui-elem-field layui-field-title">\n' +
            '            <legend><span>&nbsp;&nbsp;<a style="text-decoration: none;color: grey;font-size: 12px"  href="/home/visitor/'+item.sid+'"><b>'+item.name1+'</b>&nbsp;在&nbsp;</a><span style="font-size: 8px"><a href="/home/post/'+item.link+'">文章</a>&nbsp;中'+item.type+'了你:'+item.content+'</span></span><span>&nbsp;<button class="btn btn-primary btn-xs" id="'+item.id+'" onclick="deleteMessageComment(this.id)">知道啦</button></span><span>&nbsp;<a class="btn btn-primary btn-xs" href="/home/post/'+item.link+'">前往回复</a></span></legend>\n' +
            '        </fieldset>';
    });
    $("#comment").html(str);
}
//获取通知消息总数
function getMessageNum(){
    var nums = 0;
    nums += get_message_likes(UID).data.length;
    nums += get_message_collection(UID).data.length;
    nums += get_message_comment(UID).data.length;
    $("#message_nums").html(nums);
    message_num =nums;
}

//改变消息状态为已读
function deleteMessageLike(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        update_message_likes_status(id);
        layer.msg("已读");
        getMessageNum();
        if(message_num === 0){
            $('#message').html("<fieldset class=\"layui-elem-field\">\n" +
                "  <legend>暂无通知</legend>\n" +
                "  <div class=\"layui-field-box\">\n" +
                "    快去发帖赚取更多关注与点赞吧\n" +
                "  </div>\n" +
                "</fieldset>!");
            layer.close(index);
        }else {
            getLikesMessage();
            layer.close(index);
        }

    });
}
function deleteMessageComment(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        update_message_comment_status(id);
        layer.msg("已读");
        getMessageNum();
        if(message_num === 0){
            $('#message').html("暂无通知消息!");
            layer.close(index);
        }else {
            getCommentMessage();
            layer.close(index);
        }

    });
}
function deleteMessageCollection(id) {
    layui.use('layer', function() {
        var index = layer.load(2);
        var index = layer.load(2);
        update_message_collection_status(id);
        layer.msg("已读");
        getMessageNum();
        if(message_num === 0){
            $('#message').html("暂无通知消息!");
            layer.close(index);
        }else {
            getCollectionMessage();
            layer.close(index);
        }

    });
}
getMessageNum();
